' Demo_1.bas
#COMPILE EXE
#DIM ALL

'#console off
#INCLUDE "Win32api.inc"
#INCLUDE "PB_CommonConsoleFunctions.inc"
'
%Date = 0  ' used for elements
%Day  = 1  ' in array
'
$OutputFile = "Output Day list.csv"
'
%Max_days = 900 ' max days to process
'
FUNCTION PBMAIN () AS LONG
  '
  CON.PRINT "Hello world2"
  '
  LOCAL strLog AS STRING
  LOCAL strError AS STRING
  '
  strLog = EXE.NAME$ & "_log.txt"
  '
  TRY
    KILL strLog
  CATCH
  FINALLY
  END TRY
  '
  ' wipe the output file
  TRY
    KILL $OutputFile
  CATCH
  FINALLY
  END TRY
  '
  IF ISTRUE funAppendToAFile(strLog, _
                             "App started " & TIME$, _
                             strError) THEN
    CON.PRINT "Write to log successful"
  ELSE
    CON.PRINT "Unable to write to log -> " & strError
  END IF
  '
  LOCAL strResult AS STRING     ' string to report result
  LOCAL strDate AS STRING       ' used for start date
  LOCAL strOutputFile AS STRING ' name/path to output file
  '
  strDate = DATE$           ' set to todays date in MM/dd/yyyy
                            ' format
  strOutputFile = $OutputFile
  '
  '
  ' run the process
  IF ISTRUE funProcess(strDate, strOutputFile) THEN
    strResult = "Processing Successful "
  ELSE
    strResult = "Processing unsuccessful "
  END IF
  '
  CON.PRINT strResult
  '
  funExitApp(3)
  '
  funAppendToAFile(strLog, _
                   "App ended " & TIME$, _
                   strError)
  ' use profile command while developing to
  ' check for inefficient subroutines and functions
  'PROFILE "Profile.txt"
  '
END FUNCTION
'
FUNCTION funProcess(strDate AS STRING, _
                    strOutputFile AS STRING) AS LONG
' run the main processing
  CON.CAPTION$= "File Production"
  ' set colour to be brown
  CON.COLOR 6,0
  ' set 30 rows by 60 columns
  CON.SCREEN = 30,60
  '
  CON.PRINT "Processing"
  '
  ' output file of last 90 days
  DIM a_strDays(%Max_days,1) AS STRING
  '
  '
  LOCAL lngYear, lngMonth, lngDay AS LONG
  '
  ' populate date variables
  lngYear  = VAL(RIGHT$(strDate,4))
  lngMonth = VAL(LEFT$(strDate,2))
  lngDay   = VAL(MID$(strDate,4,2))
  '
  ' prepare date class
  LOCAL ipDate AS IPOWERTIME
  LET ipDate = CLASS "PowerTime
  ' set the date
  ipDate.NewDate(lngYear,lngMonth,lngDay)
  '
  ' prepare to step back 90 days
  LOCAL lngD AS LONG
  LOCAL strError AS STRING
  '
  ' output headers to file when saving row by row
  'funAppendToAFile(strOutputFile, _
  '                 $DQ & "Date" & $QCQ & "Day of Week" & $DQ, _
  '                 strError)

  WHILE lngD < UBOUND(a_strDays,1)
  'for lngD = 1 to 90
    ' step back a day
    ipDate.AddDays(-1)
    '
    ' keep only weekdays
    SELECT CASE ipDate.DayOfWeekString
      CASE "Sunday","Saturday"
        ITERATE
      CASE ELSE
        INCR lngD
    END SELECT
    '
    ' populate the array with data
    a_strDays(lngD,%Date) = ipDate.DateString
    a_strDays(lngD,%Day)  = ipDate.DayOfWeekString
    '
    ' save to file row by row
    'funAppendToAFile(strOutputFile, _
    '               $DQ & a_strDays(lngD,%Date) & $QCQ & _
    '               a_strDays(lngD,%Day) & $DQ, _
    '               strError)
    'if strError <> "" then
    '  exit loop
    'end if
    '
   WEND
   '
   ' test for errors when saving row by row
   'if strError = "" then
   '  function = %TRUE
   'end if

  ' save entire array to disk - more efficient
  IF ISTRUE funSaveArray(strOutputFile,a_strDays(), _
                         strError) THEN
    CON.PRINT "File saved successfully"
    FUNCTION = %TRUE
  ELSE
    CON.PRINT "File unable to be saved " & strError
  END IF
  '
END FUNCTION
'
FUNCTION funSaveArray(strOutputFile AS STRING, _
                      BYREF a_strDays() AS STRING, _
                      strError AS STRING) AS LONG
                      '
  LOCAL lngFileOut AS LONG  ' output file handle
  LOCAL lngD AS LONG
  lngFileOut = FREEFILE     ' assign a handle
  '
  ' output the array to file
  TRY
    ERRCLEAR ' clear any existing errors
    OPEN strOutputFile FOR OUTPUT AS #lngFileOut
    ' print out the headers
    PRINT #lngFileOut,$DQ & "Date" & $QCQ;
    PRINT #lngFileOut,"Day of Week" & $DQ
    '
    FOR lngD = 1 TO UBOUND(a_strDays,1)
    ' output each row of data in CSV format
      PRINT #lngFileOut,$DQ & a_strDays(lngD,%Date) & $QCQ;
      PRINT #lngFileOut,a_strDays(lngD,%Day) & $DQ
    NEXT lngD
    FUNCTION = %TRUE
    '
  CATCH
  ' return false if output errors
    strError = ERROR$(ERR) ' return the error string
    FUNCTION = %FALSE
  FINALLY
  ' ensure we close the file
    CLOSE #lngFileOut
  END TRY
  '
END FUNCTION